//
//  ViewController.swift
//  qsactivityView
//
//  Created by 火星人 on 2017/11/2.
//  Copyright © 2017年 火星人. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    
    let indicator = QSActivityIndicatorView()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        indicator.frame = CGRect(origin:
            CGPoint(x: 100, y: 100), size: CGSize(width: 100, height: 100))
        self.view.addSubview(indicator)
        
        indicator.backgroundColor = UIColor.darkGray
        indicator.progressTintColor = UIColor.white
        indicator.roundBlockStyle = false
        indicator.resetUI()
    }
    @IBAction func scrollview(_ sender: Any) {
        
        let vc = TestScrollVC()
        self.navigationController?.pushViewController(vc, animated: true)
    }
    @IBAction func refresh(_ sender: Any) {
        
        let vc = TestVC()
        self.navigationController?.pushViewController(vc, animated: true)
        
    }
    @IBAction func start(_ sender: Any) {
        if indicator.isAnimating {
            indicator.pause()
        }
        else{
            indicator.start()
        }
    }
    
    var preX: CGFloat  = 0
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        for t in touches {
            preX = t.location(in: self.view).x
        }
    }
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesMoved(touches, with: event)
        for t in touches {
            let x = t.location(in: self.view).x
            
            let p = (x - preX) / 200
            if p > 1 {
                indicator.spin()
            }
            else{
                indicator.progress = p
            }
        }
    }
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesEnded(touches, with: event)
        indicator.finish()
    }
    
}

